﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CVsharp.Models.Entities;

namespace CVsharp.Models.DAL.Infrastructure
{
    public enum SysLogType
    {
        /// <summary>
        /// 系统日志
        /// </summary>
        Sys = 0,
        /// <summary>
        /// 登录
        /// </summary>
        Login=1,
        /// <summary>
        /// 登出
        /// </summary>
        LogOff=2,
        /// <summary>
        /// 系统错误
        /// </summary>
        Error = -1
    }
    public class SysLog 
    {
        public bool Write(string userName, SysLogType type, string content)
        {
            using (DBEntities db = new DBEntities())
            {
                sysLog log = new sysLog
                                 {
                                     LogID = Guid.NewGuid(),
                                     GreateTime = DateTime.Now,
                                     UserName = userName,
                                     Type = GetStrBySysLogType(type),
                                     LogContent = content
                                 };
                db.sysLog.AddObject(log);
                int result = db.SaveChanges();
                return result == 1;
            }
        }
        private string GetStrBySysLogType(SysLogType sysLogType)
        {
            switch (sysLogType)
            {
                case SysLogType.Error:
                    return "系统错误";
                case SysLogType.Login:
                    return "用户登录";
                case SysLogType.LogOff:
                    return "安全退出";
                case SysLogType.Sys:
                    return "系统日志";
            }
            return "未知类型";
        }
    }
}
